package com.su.edu.api.web;

import com.su.edu.api.converter.UserVoConverter;
import com.su.edu.api.model.req.UserReq;
import com.su.edu.api.model.res.UserVo;
import com.su.edu.application.model.UserDto;
import com.su.edu.application.service.UserService;
import com.su.edu.common.model.BaseRes;
import com.su.edu.common.model.ValidGroup.InsertGroup;
import com.su.edu.common.model.ValidGroup.UpdateGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author wangjun
 * @since 2024/6/5 14:04
 */
@Slf4j
@RestController
@RequestMapping("/api/v1")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserVoConverter userVoConverter;

    @GetMapping("/user/{id}")
    public BaseRes<UserVo> findUser(@PathVariable(value = "id") Long id) {
        //日志打印
        log.info("request find user:{}. ", id);
        UserDto userDto = userService.findById(id);
        log.info("request find user:{}. response", userDto);
        return BaseRes.success(userVoConverter.toVo(userDto));
    }

    @PostMapping("/user/add")
    public BaseRes<UserVo> add(@Validated(InsertGroup.class) @RequestBody UserReq userReq) {
        log.info("request add user:{}. ", userReq);
        UserDto userDto = userVoConverter.toDto(userReq);
        userService.addUser(userDto);
        return BaseRes.success(userVoConverter.toVo(userDto));
    }

    @PutMapping("/user/modify")
    public BaseRes<UserVo> modify(@Validated(UpdateGroup.class) @RequestBody UserReq userReq) {
        log.info("request modify user:{}. ", userReq);
        UserDto userDto = userVoConverter.toDto(userReq);
        userService.modifyUser(userDto);
        return BaseRes.success(userVoConverter.toVo(userDto));
    }
}
